package com.oj.ojbackendjudgeservice.judge.strategy;

import cn.hutool.json.JSONUtil;
import ojbackendmodel.model.codesandbox.JudgeInfo;
import ojbackendmodel.model.dto.question.JudgeCase;
import ojbackendmodel.model.dto.question.JudgeConfig;
import ojbackendmodel.model.entity.Question;
import ojbackendmodel.model.entity.QuestionSubmit;
import ojbackendmodel.model.enums.JudgeInfoMessageEnum;

import java.util.List;
import java.util.Optional;

public class DefaultJudgeStrategy implements JudgeStrategy{
    @Override
    public JudgeInfo doJudge(JudgeContext judgeContext) {
        // 默认值是通过
        JudgeInfoMessageEnum judgeInfoMessageEnum = JudgeInfoMessageEnum.ACCEPTED;
        JudgeInfo judgeInfo = judgeContext.getJudgeInfo();
        List<String> inputList = judgeContext.getInputList();
        List<String> outputList = judgeContext.getOutputList();
        List<JudgeCase> judgeCaseList = judgeContext.getJudgeCaseList();
        Question question = judgeContext.getQuestion();
        QuestionSubmit questionSubmit = judgeContext.getQuestionSubmit();


        Long memory = Optional.ofNullable(judgeInfo.getMemory()).orElse(0L);
        Long time = Optional.ofNullable(judgeInfo.getTime()).orElse(0L);
        JudgeInfo judgeInfoResponse = new JudgeInfo();
        judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
        judgeInfoResponse.setMemory(memory / (1024 * 1024));
        judgeInfoResponse.setTime(time);
        // 1.先判断沙箱执行的结果输出数量是否和预期输出数量相等
        if (outputList.size() != inputList.size()){
            System.out.println("输出溢出啦");
            judgeInfoMessageEnum = JudgeInfoMessageEnum.OUTPUT_LIMIT_EXCEEDED;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoResponse;
        }

        // 2.依此判断每一项输出和预期输出是否相等
        for (int i = 0; i < judgeCaseList.size(); i++) {
            JudgeCase judgeCase1 = judgeCaseList.get(i);
            if (!judgeCase1.getOutput().equals(outputList.get(i))){
                System.out.println("答案错误啦");
                judgeInfoMessageEnum = JudgeInfoMessageEnum.WRONG_ANSWER;
                judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
                return judgeInfoResponse;
            }
        }

        // 3.判断题目的限制是否符合要求
        String judgeConfigStr = question.getJudgeConfig();
        JudgeConfig judgeConfig = JSONUtil.toBean(judgeConfigStr, JudgeConfig.class);
        Long needMemoryLimit = judgeConfig.getMemoryLimit();
        Long needTimeLimit = judgeConfig.getTimeLimit();
        if ((memory / (1024 * 1024)) > needMemoryLimit){
            System.out.println("内存超出啦" + needMemoryLimit + "---" + (memory / (1024 * 1024)) + "MB");
            judgeInfoMessageEnum = JudgeInfoMessageEnum.MEMORY_LIMIT_EXCEEDED;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoResponse;
        }
        if ((time / 1000) > needTimeLimit){
            System.out.println("时间超出啦" + needTimeLimit + "---" + (time / 1000) + "s");
            judgeInfoMessageEnum = JudgeInfoMessageEnum.TIME_LIMIT_EXCEEDED;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoResponse;
        }


        return judgeInfoResponse;
    }
}
